﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using CardLogistics.Model.Basic;
using System.Data.OracleClient;
using System.Data;
using System.Collections;

namespace CardLogistics.DAL.Basic
{
    /// <summary>
    /// 供应商数据访问类
    /// </summary>
    public class Basic_SuppliersData
    {
        #region 查询
        /// <summary>
        /// 根据条件查询相应的表的字段
        /// </summary>
        /// <param name="StrWhere">查询条件</param>
        /// <param name="StrFields">要查询的字段</param>
        /// <returns>填充完数据的DataTable</returns>
        public DataTable GetList(string StrWhere, string StrFields)
        {
            return CommonData.GetListOrderBy(StrWhere, "BASIC_SUPPLIERS", StrFields, "SUPPLIERSID", 0);
        }

        /// <summary>
        /// 根据编号查询基本信息
        /// </summary>
        /// <param name="id">编号</param>
        /// <returns>填充完数据的DataTable</returns>
        public DataTable GetInfoById(string id)
        {
            return GetList(" SUPPLIERSID = '" + id + "' ", "*");
        }

        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public Basic_Suppliers GetModel(string rId)
        {
            Basic_Suppliers model = new Basic_Suppliers();
            DataRow row = GetInfoById(rId).Rows[0];
            if (row != null)
            {
                if (row["SUPPLIERSNO"] != null)
                {
                    model.SUPPLIERSID = row["SUPPLIERSID"].ToString();
                }
                if (row["SUPPLIERSNO"] != null)
                {
                    model.SUPPLIERSNO = row["SUPPLIERSNO"].ToString();
                }
                if (row["SUPPLIERSNAME"] != null)
                {
                    model.SUPPLIERSNAME = row["SUPPLIERSNAME"].ToString();
                }
                if (row["SHORTNAME"] != null)
                {
                    model.SHORTNAME = row["SHORTNAME"].ToString();
                }
                if (row["SUPPLIERSCODE"] != null)
                {
                    model.SUPPLIERSCODE = row["SUPPLIERSCODE"].ToString();
                }
                if (row["CONTACTPERSON"] != null)
                {
                    model.CONTACTPERSON = row["CONTACTPERSON"].ToString();
                }
                if (row["CONTACTPHONE"] != null)
                {
                    model.CONTACTPHONE = row["CONTACTPHONE"].ToString();
                }
                if (row["CONTACTADDRESS"] != null)
                {
                    model.CONTACTADDRESS = row["CONTACTADDRESS"].ToString();
                }
                if (row["PRINCIPAL"] != null)
                {
                    model.PRINCIPAL = row["PRINCIPAL"].ToString();
                }
                if (row["PRINCIPALPHONE"] != null)
                {
                    model.PRINCIPALPHONE = row["PRINCIPALPHONE"].ToString();
                }
                if (row["BOXNUMBER"] != null && row["BOXNUMBER"].ToString()!="")
                {
                    model.BOXNUMBER = int.Parse(row["BOXNUMBER"].ToString());
                }
                if (row["CARDNUMBER"] != null && row["CARDNUMBER"].ToString() !="")
                {
                    model.CARDNUMBER = int.Parse(row["CARDNUMBER"].ToString());
                }
                if (row["ADDTIME"] != null && row["ADDTIME"].ToString()!="")
                {
                    model.ADDTIME = DateTime.Parse(row["ADDTIME"].ToString());
                }
                if (row["LASTUPDATEUSER"] != null)
                {
                    model.LASTUPDATEUSER = row["LASTUPDATEUSER"].ToString();
                }
                if (row["LASTUPDATEDATE"] != null && row["LASTUPDATEDATE"].ToString() != "")
                {
                    model.LASTUPDATEDATE = DateTime.Parse(row["LASTUPDATEDATE"].ToString());
                }
                if (row["STATUS"] != null && row["STATUS"].ToString()!="")
                {
                    model.STATUS = int.Parse(row["STATUS"].ToString());
                }
                if (row["ISDELETE"] != null && row["ISDELETE"].ToString() != "")
                {
                    model.ISDELETE = int.Parse(row["ISDELETE"].ToString());
                }
                if (row["BANKACCOUNT"] != null)
                {
                    model.BANKACCOUNT = row["BANKACCOUNT"].ToString();
                }
                if (row["ACCOUNTNUMBER"] != null)
                {
                    model.ACCOUNTNUMBER = row["ACCOUNTNUMBER"].ToString();
                }
                if (row["FAX"]!= null)
                {
                    model.FAX = row["FAX"].ToString();
                }
            }
            return model;
        }
        #endregion

        #region 新增
        /// <summary>
        /// 新增信息
        /// </summary>
        /// <param name="model">实体类</param>
        /// <returns>返回插入数据的编号</returns>
        public string Add(Basic_Suppliers model)
        {
            try
            {
                OracleParameter[] parameters = {
                    new OracleParameter("v_varOut", OracleType.VarChar,50),
                    new OracleParameter("v_suppliersno", OracleType.VarChar,50),
                    new OracleParameter("v_suppliersname", OracleType.VarChar,200),
                    new OracleParameter("v_shortname", OracleType.VarChar,100),
                    new OracleParameter("v_supplierscode", OracleType.VarChar,100),
                    new OracleParameter("v_contactperson", OracleType.VarChar,50),
                    new OracleParameter("v_contactphone", OracleType.VarChar,50),
                    new OracleParameter("v_contactaddress", OracleType.VarChar,500),
                    new OracleParameter("v_principal", OracleType.VarChar,50),
                    new OracleParameter("v_principalphone", OracleType.VarChar,50),
                    new OracleParameter("v_boxnumber", OracleType.Number),
                    new OracleParameter("v_cardnumber", OracleType.Number),
                    new OracleParameter("v_lastupdateuser", OracleType.VarChar,50),
                    new OracleParameter("v_lastupdatedate", OracleType.DateTime),
                    new OracleParameter("v_status", OracleType.Number),
                    new OracleParameter("v_isdelete", OracleType.Number),
                    new OracleParameter("v_bankaccount", OracleType.VarChar,100),
                    new OracleParameter("v_accountnumber", OracleType.VarChar,100),
                    new OracleParameter("v_fax",OracleType.VarChar,50)};
                parameters[0].Direction = ParameterDirection.Output;
                parameters[1].Value = model.SUPPLIERSNO;
                parameters[2].Value = model.SUPPLIERSNAME;
                parameters[3].Value = model.SHORTNAME;
                parameters[4].Value = model.SUPPLIERSCODE;
                parameters[5].Value = model.CONTACTPERSON;
                parameters[6].Value = model.CONTACTPHONE;
                parameters[7].Value = model.CONTACTADDRESS;
                parameters[8].Value = model.PRINCIPAL;
                parameters[9].Value = model.PRINCIPALPHONE;
                parameters[10].Value = model.BOXNUMBER;
                parameters[11].Value = model.CARDNUMBER;
                parameters[12].Value = model.LASTUPDATEUSER;
                parameters[13].Value = model.LASTUPDATEDATE;
                parameters[14].Value = model.STATUS;
                parameters[15].Value = model.ISDELETE;
                parameters[16].Value = model.BANKACCOUNT;
                parameters[17].Value = model.ACCOUNTNUMBER;
                parameters[18].Value = model.FAX;
                string StrOutPar = DbHelperSQL.RunProcedure("P_basic_suppliers_ADD", parameters, parameters[0]).ToString();
                return StrOutPar;
            }
            catch (Exception ex)
            {
                return "err," + ex.Message;
            }
        }
        #endregion

        #region 修改
        /// <summary>
        /// 修改信息
        /// </summary>
        /// <param name="model">实体类</param>
        /// <returns>返回插入数据的编号</returns>
        public string Update(Basic_Suppliers model)
        {
            try
            {
                OracleParameter[] parameters = {
                    new OracleParameter("v_varOut", OracleType.VarChar,50),
                    new OracleParameter("v_suppliersid", OracleType.VarChar,50),
                    new OracleParameter("v_suppliersno", OracleType.VarChar,50),
                    new OracleParameter("v_suppliersname", OracleType.VarChar,200),
                    new OracleParameter("v_shortname", OracleType.VarChar,100),
                    new OracleParameter("v_supplierscode", OracleType.VarChar,100),
                    new OracleParameter("v_contactperson", OracleType.VarChar,50),
                    new OracleParameter("v_contactphone", OracleType.VarChar,50),
                    new OracleParameter("v_contactaddress", OracleType.VarChar,500),
                    new OracleParameter("v_principal", OracleType.VarChar,50),
                    new OracleParameter("v_principalphone", OracleType.VarChar,50),
                    new OracleParameter("v_boxnumber", OracleType.Number),
                    new OracleParameter("v_cardnumber", OracleType.Number),
                    new OracleParameter("v_lastupdateuser", OracleType.VarChar,50),
                    new OracleParameter("v_lastupdatedate", OracleType.DateTime),
                    new OracleParameter("v_status", OracleType.Number),
                    new OracleParameter("v_bankaccount", OracleType.VarChar,100),
                    new OracleParameter("v_accountnumber", OracleType.VarChar,100),
                    new OracleParameter("v_fax",OracleType.VarChar,50)};
                parameters[0].Direction = ParameterDirection.Output;
                parameters[1].Value = model.SUPPLIERSID;
                parameters[2].Value = model.SUPPLIERSNO;
                parameters[3].Value = model.SUPPLIERSNAME;
                parameters[4].Value = model.SHORTNAME;
                parameters[5].Value = model.SUPPLIERSCODE;
                parameters[6].Value = model.CONTACTPERSON;
                parameters[7].Value = model.CONTACTPHONE;
                parameters[8].Value = model.CONTACTADDRESS;
                parameters[9].Value = model.PRINCIPAL;
                parameters[10].Value = model.PRINCIPALPHONE;
                parameters[11].Value = model.BOXNUMBER;
                parameters[12].Value = model.CARDNUMBER;
                parameters[13].Value = model.LASTUPDATEUSER;
                parameters[14].Value = model.LASTUPDATEDATE;
                parameters[15].Value = model.STATUS;
                parameters[16].Value = model.BANKACCOUNT;
                parameters[17].Value = model.ACCOUNTNUMBER;
                parameters[18].Value = model.FAX;
                string StrOutPar = DbHelperSQL.RunProcedure("P_basic_suppliers_UPDATE", parameters, parameters[0]).ToString();
                return StrOutPar;
            }
            catch (Exception ex)
            {
                return "err," + ex.Message;
            }
        }
        #endregion

        #region 删除
        /// <summary>
        /// 删除信息
        /// </summary>
        /// <param name="id">删除信息的ID</param>
        /// <returns>删除成功返回True。否则返回false</returns>
        public string Delete(string id)
        {
            try
            {
                OracleParameter[] parameters = {
                        new OracleParameter("VarOut",  OracleType.VarChar,100),
					    new OracleParameter("v_suppliersid",  OracleType.VarChar,50)
                };
                parameters[0].Direction = ParameterDirection.Output;
                parameters[1].Value = id;
                string StrOutPar = DbHelperSQL.RunProcedure("P_basic_suppliers_Delete", parameters, parameters[0]).ToString();
                return StrOutPar;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        #endregion
    }
}
